<?php

namespace RjYst\Category;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;

class RjYstCategoryServiceProvider extends ServiceProvider
{
    /**
     * 注册服务
     */
    public function register()
    {
        // 合并配置文件
        $this->mergeConfigFrom(
            __DIR__.'/../config/rjyst-category.php', 'rjyst-category'
        );
    }

    /**
     * 启动服务
     */
    public function boot()
    {
        // 发布配置文件
        $this->publishes([
            __DIR__.'/../config/rjyst-category.php' => config_path('rjyst-category.php'),
        ], 'rjyst-category-config');

        // 发布迁移文件
        $this->publishes([
            __DIR__.'/../database/migrations' => database_path('migrations'),
        ], 'rjyst-category-migrations');

        // 发布数据填充器（修正后的路径）
        $this->publishes([
            __DIR__.'/../database/seeders/CategorySeeder.php' => database_path('seeders/CategorySeeder.php'),
            __DIR__.'/../database/seeders/AdminMenuSeeder.php' => database_path('seeders/AdminMenuSeeder.php'),
        ], 'rjyst-category-seeders');

        // 发布模型文件
        $this->publishes([
            __DIR__.'/Models/Category.php' => app_path('Models/Category/Category.php'),
        ], 'rjyst-category-models');

        // 发布控制器文件
        $this->publishes([
            __DIR__.'/Http/Controllers/CategoryController.php' => app_path('Admin/Controllers/Category/CategoryController.php'),
        ], 'rjyst-category-controllers');

        // 发布仓库文件
        $this->publishes([
            __DIR__.'/Repositories/Category.php' => app_path('Admin/Repositories/Category/Category.php'),
        ], 'rjyst-category-repositories');

        // 发布所有文件（修正后）
        $this->publishes([
            __DIR__.'/../config/rjyst-category.php' => config_path('rjyst-category.php'),
            __DIR__.'/../database/migrations' => database_path('migrations'),
            __DIR__.'/../database/seeders/CategorySeeder.php' => database_path('seeders/CategorySeeder.php'),
            __DIR__.'/../database/seeders/AdminMenuSeeder.php' => database_path('seeders/AdminMenuSeeder.php'),
            __DIR__.'/Models/Category.php' => app_path('Models/Category/Category.php'),
            __DIR__.'/Http/Controllers/CategoryController.php' => app_path('Admin/Controllers/Category/CategoryController.php'),
            __DIR__.'/Repositories/Category.php' => app_path('Admin/Repositories/Category/Category.php'),
        ], 'rjyst-category-all');

        // 加载数据库迁移（从包内）
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');

        // 注册路由
        $this->registerRoutes();

        // 如果在控制台运行，注册命令
        if ($this->app->runningInConsole()) {
            $this->commands([
                \RjYst\Category\Console\Commands\InstallCategoryCommand::class,
                \RjYst\Category\Console\Commands\UninstallCategoryCommand::class,
            ]);
        }
    }

    /**
     * 注册路由
     */
    protected function registerRoutes()
    {
        if (config('rjyst-category.auto_register_routes', true)) {
            Route::group($this->routeConfiguration(), function () {
                $this->loadRoutesFrom(__DIR__.'/../routes/admin.php');
            });
        }
    }

    /**
     * 路由配置
     */
    protected function routeConfiguration()
    {
        return [
            'prefix' => config('admin.route.prefix', 'admin'),
            'namespace' => config('admin.route.namespace', 'App\\Admin\\Controllers'),
            'middleware' => config('admin.route.middleware', ['web', 'admin']),
        ];
    }
}
